package chaoyue.study.aop;

import chaoyue.study.annotation.MyCustomAnnotation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class MyCustomAspect {
    //指定自定义注解为切入点
//    @Pointcut("@annotation(customAnnotation)")
//    public void customAnnotationPointcut(MyCustomAnnotation customAnnotation) {}

    
    @Before("@annotation(customAnnotation)")
    public void beforeAdvice(MyCustomAnnotation customAnnotation) {
        System.out.println("前置增强: " + customAnnotation.value1() + " - " + customAnnotation.value2());
    }

    @After("@annotation(customAnnotation)")
    public void afterAdvice(MyCustomAnnotation customAnnotation) {
        System.out.println("后置增强: " + customAnnotation.value1() + " - " + customAnnotation.value2());
    }

    @Around("@annotation(customAnnotation)")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint, MyCustomAnnotation customAnnotation) throws Throwable {
        long startTime = System.currentTimeMillis();

        Object result = joinPoint.proceed();

        long endTime = System.currentTimeMillis();
        long executionTime = endTime - startTime;

        log.info("Method: " + joinPoint.getSignature().getName());
        log.info("Execution Time: " + executionTime + "ms");
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            log.info("Parameter " + (i + 1) + ": " + args[i]);
        }
        log.info("Annotation: " + customAnnotation.value1() + " - " + customAnnotation.value2());

        return result;
    }
}
